/**
 * 引入了所有需要的头文件，
 * 定义一些基本的变量、方法。
*/
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <iomanip>
#include <map>

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <pthread.h>
#include <signal.h>

using namespace std;

//定义KB的大小
#define KB 0x400
//定义i节点总数
#define INODENUM 2048
//定义文件块总数
#define BLKNUM 100337
//定义i节点起始位置
#define INODESTART 15
//定义文件块起始位置
#define BLKSTART 2063
//定义目录i节点标记
#define FOLDERSIGN 0x0F000000
//定义共享内存key
#define MEMKEY 2333
//定义共享内存结构体workpath、cmd字段大小
#define MEMTEXT_SZ 500
//定义共享内存结构体output字段大小
#define MEMOUT_SZ 9999
//定义回复标记
#define REPLYSIGN 16
//定义是否打开debug模式
#define DEBUG false
//定义磁盘名称
const string FNAME = "disk";

/**
 * 将char转为非负整数
*/
int castChar2Int(char c)
{
    unsigned char uc = c;
    int i = (int)uc;
    if (DEBUG)
    {
        cout << '[' << __FUNCTION__ << ']';
        cout << "cast: " << i << endl;
    }
    return i;
}

/**
 * 将8位二进制字符数组转换为char
*/
char castBin2Char(char *byteBin)
{
    int size = strlen(byteBin);
    char bit;
    int byteint = 0;
    for (int i = 0; i < size - 1; ++i)
    {
        bit = byteBin[i];
        if (bit == '1')
        {
            byteint++;
        }
        byteint *= 2;
    }
    bit = byteBin[size - 1];
    if (bit == '1')
    {
        byteint++;
    }
    char bytechar = byteint;
    if (DEBUG)
    {
        cout << '[' << __FUNCTION__ << ']';
        cout << "cast: " << byteBin << "-->" << bytechar << endl;
    }
    return bytechar;
}

/**
 * 将char转换为8位二进制字符串
*/
string castChar2Bin(char c)
{
    int byteint = castChar2Int(c);
    string bin = "";
    for (int i = 0; i < 8; ++i)
    {
        if (byteint % 2 == 0)
        {
            bin = "0" + bin;
        }
        else
        {
            bin = "1" + bin;
        }
        byteint /= 2;
    }
    if (DEBUG)
    {
        cout << '[' << __FUNCTION__ << ']';
        cout << "cast: " << c << "-->" << bin << endl;
    }
    return bin;
}

/**
 * 将整形转换为二进制字符串
*/
string int2BinStr(int a)
{
    string bin = "";
    int aa = a;
    while (a > 0)
    {
        if (a % 2 == 0)
        {
            bin = "0" + bin;
        }
        else
        {
            bin = "1" + bin;
        }
        a /= 2;
    }
    if (DEBUG)
    {
        cout << '[' << __FUNCTION__ << ']';
        cout << aa << ' ' << bin << endl;
    }
    return bin;
}

/**
 * 获取int时间
*/
int getIntTime()
{
    time_t t;
    int tint = (int)time(&t);
    return tint;
}

/**
 * 获取带格式的string时间
*/
string getStringTime()
{
    time_t t;
    int tint = (int)time(&t);
    struct tm tm;
    char s[100];
    tm = *localtime(&t);
    strftime(s, sizeof(s), "%Y%m%d-%H:%M:%S", &tm);
    string str = s;
    return str;
}

/**
 * 将int时间戳转换为带格式的字符串
*/
string timeToString(int timestamp)
{
    time_t t = (time_t)timestamp;
    struct tm tm;
    char s[100];
    tm = *localtime(&t);
    strftime(s, sizeof(s), "%Y%m%d-%H:%M:%S", &tm);
    string str = s;
    return str;
}

/**
 * 读取一字节中特定位
 * @param pos 1~8
*/
bool readBitInChar(char c, int pos) 
{
    if(pos<1 || pos>8){
        cout << '[' << __FUNCTION__ << ']';
        cout << "pos not correct!" << endl;
    }
    int cint=castChar2Int(c);
    //cout<<"cint:"<<cint<<endl;
    //cout<<castChar2Bin<<endl;
    return (cint&(1<<(8-pos)));
}

/**
 * 按毫秒休眠
*/
void sleepms(int ms){
    ms *= 1000;
    usleep(ms);
}